<?php
	namespace Model;
	use Think\Model;
	class DrawModel extends Model{
        protected $tableName = 'user';
	    //检测数据库中是否含有该用户，没有则新增，返回用户信息数组
		public static function adduser($data){
//            var_dump($data);
            $drawmodel=new \Model\DrawModel();
            if($user=$drawmodel->where("openid='".$data['openid']."' ")->find()){
                session("uid",$user['uid']);
                session('name',$user['name']);
                session('openid',$user['openid']);
                return $user;
            }else{
                $user=["name"=>$data['nickname'],"password"=>"","status"=>0,"sex"=>$data['sex'],"main_img"=>$data['headimgurl'],"openid"=>$data['openid'],"country"=>$data['country'],"city"=>$data['city']];
                $uid=$drawmodel->add($user);
                session("uid",$user['uid']);
                session('name',$user['name']);
                session('openid',$user['openid']);
                return $drawmodel->where("uid={$uid}")->find();
            }

//            D('user')

        }
        //创建房间
        public static function create_room($user_uid){
		    //只允许创建一个房间
            $room=D("room")->where("host_uid=".$user_uid)->find();
            if($room){
                return $room['uid'];
            }
            return D("room")->add(array("host_uid"=>$user_uid,"user_uids"=>$user_uid,"has_ready_uids"=>$user_uid));
        }

        //开始游戏，添加用户uid到表
        public static function start($room_uid){
            $room=D("room")->where("uid=".$room_uid)->find();
            //检测用户是否全部已经准备
            $all_users=explode(",",$room['user_uids']);
            $ready_users=explode(",",$room['has_ready_uids']);
//            var_dump(count(array_intersect($all_users,$ready_users)));exit;
            $samecount=count(array_intersect($all_users,$ready_users));
//            echo $samecount;
////            echo "s";
//            echo count($all_users);
            if(count($all_users)<2){
                return array("error"=>0,"course"=>"至少需要两个人才可以开始游戏");
            }
            if($samecount!=count($all_users)){
                return array("error"=>0,"course"=>"全部人准备后才可以开始游戏");
            }
//            $user_uids=$room['user_uids'];
		    //清空用户分数
            $db=new \Model\DbModel();
            $db->updatedata("user",array("point"=>0),"uid in(".$room['user_uids'].")");
//            echo $room['user_uids'];
//		    $users=explode(",",$user_uids);
            $count=count($all_users);
            $room=["uid"=>$room_uid,"paint_uid"=>$all_users[0],"user_count"=>$count,"is_playing"=>1];
//            $drawmodel=new \Model\DrawModel();

            return array("error"=>1,"course"=>D("room")->save($room));
        }
        //检测房间是否已经开始游戏
        public static function is_playing($room_uid){
            $flag=D('room')->field("is_playing")->where("uid=".$room_uid)->find();
            return $flag['is_playing'];
        }
        //获取房间用户列表信息
        public static function get_room_users($room_uid){
            $room=D('room')->field("user_uids,host_uid")->where("uid=".$room_uid)->find();
//            $user_arr=explode(",",$room['user_uids']);
//            $user_info_arr=[];
//            foreach ($user_arr as $k=>$v){
//                $user_info_arr[]=
//            }
//            if()
            $user_arr=D("user")->where("uid in (".$room['user_uids'].") and uid!=".$room["host_uid"])->select();
            $user_arr["host"]=D("user")->where("uid=".$room["host_uid"])->find();
            return $user_arr;
        }
        //下一轮,
        public static function next($room_uid){
            $room=D('room')->where("uid=".$room_uid)->find();
            $users=explode(",",$room['user_uids']);
            $index=array_search($room['paint_uid'],$users);
//            var_dump($index);
            $db=new \Model\DbModel();
            $nextindex=($index+1)>(count($users)-1)?0:$index+1;
//            echo $nextindex;exit;
            $db->updatedata("room",array("paint_uid"=>$users[$nextindex]),"uid=".$room_uid);
            $db->updatedataforint("room",array("now_term"=>"now_term+"."1"),"uid=".$room_uid);

        }
        //获取房间列表和房内用户名
        public static function get_room_list(){
            $rooms=D('room')->field("uid,user_uids,is_playing")->select();
//            var_dump($rooms);
            foreach ($rooms as $k=>$v){
                if($v['user_uids']){
                    $users=D('user')->field("uid,name,main_img")->where("uid in(".$v['user_uids'].")")->select();
                    $rooms[$k]['users']=$users;
                }
            }
            return $rooms;
        }

        //进入房间
        public static function inroom($uid,$user_uid){
		    if($user_uid&&$uid){
                $room=D('room')->where("uid=".$uid)->find();
                $users=explode(",",$room['user_uids']);
                array_push($users,$user_uid);
                $users=array_unique($users);//去重
                $db=new \Model\DbModel();
                $db->updatedata("room",array("user_uids"=>implode(",",$users)),"uid=".$uid);
            }

        }
	}
?>